<template>
	<view class="fix-top-window">
		<view class="uni-header">
			<uni-ad-admin-breadcrumb />
			<view class="uni-group">
				<input type="text" placeholder="请输入AppID" v-model.trim="appid" class="uni-search input-search"
					maxlength="30" />
				<button type="primary" class="uni-button" size="mini" @click="btnSearch">搜索</button>
				<button type="default" class="uni-button" size="mini" @click="btnReset">重置</button>
			</view>
		</view>
		<view class="uni-container">

			<unicloud-db ref="udb" :collection="tableName" :where="where"
				page-data="replace" :orderby="orderby" :getcount="true" :page-size="options.pageSize"
				:page-current="options.pageCurrent" v-slot:default="{ data, pagination, loading, error, options }"
				:options="options" loadtime="manual" @load="onqueryload">
				<uni-table ref="table" :loading="loading" :emptyText="error.message || loading ? '请求中...' : '没有更多数据'"
					border stripe type="" style="min-height: 600px;">
					<uni-tr>
						<uni-th align="center">#</uni-th>
						<uni-th align="center">AppID</uni-th>
						<uni-th align="center">应用名称</uni-th>
						<uni-th align="center">操作</uni-th>
					</uni-tr>
					<uni-tr v-for="(item,index) in data" :key="index">
						<uni-td align="center">{{ parseInt((index+1) + (pagination.current-1) * pagination.size) }}
						</uni-td>
						<uni-td align="center">{{item.appid}}</uni-td>
						<uni-td align="center">{{item.name}}</uni-td>
						<uni-td align="center">
							<button type="primary" v-if="item.appid" size="mini"
								@click="showDetail(item)">查看收益</button>
						</uni-td>
					</uni-tr>
				</uni-table>
				<view class="uni-pagination-box">
					<uni-pagination show-icon :page-size="pagination.size" v-model="pagination.current"
						:total="pagination.count" @change="onPageChanged" />
				</view>
			</unicloud-db>

		</view>

		<!-- #ifndef H5 -->
		<fix-window />
		<!-- #endif -->
	</view>
</template>

<script>
	const db = uniCloud.database()
	const dbCmd = db.command
	const $ = dbCmd.aggregate
	const ADREGION = 2
	import {
		fieldsMap
	} from './fieldsMap.js'
	export default {
		data() {
			return {
				loading: false,
				tableName: 'opendb-app-list',
				options: {
					pageSize: 15,
					pageCurrent: 1,
				},
				appid: '',
				where: {}
			}
		},
		onReady() {
			this.loadData()
		},
		methods: {
			showDetail(row){
				uni.navigateTo({
					url: '../detail/detail?appid=' + row.appid
				})
			},
			onPageChanged(e) {
				this.$refs.udb.loadData({
					current: e.current
				})
			},
			btnReset() {
				this.appid = ''
				this.where = {}

				this.$nextTick(() => {
					this.loadData()
				})
			},
			btnSearch() {
				this.where = {
					appid: this.appid
				}

				this.$nextTick(() => {
					this.loadData()
				})
			},
			loadData(clear = true) {
				this.$refs.udb.loadData({
					clear
				})
			},
		}
	}
</script>

<style lang="scss">
	@import "@/uni_modules/uni-ad-admin/common/uni-ad-admin.scss";
</style>